<?php

namespace App\Store\Controllers\OldMaterial;

use App\Common\Services\CommonExcelExport;
use App\Store\Models\OldMaterial\Log;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Maatwebsite\Excel\Facades\Excel;

/**
 * 旧料日志
 */
class LogController extends Controller
{
    /**
     * 列表
     */
    public function getlist(Request $request)
    {
        $list = Log::getList($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    /**
     * 列表导出
     */
    public function doexport(Request $request)
    {
        $request->isgetall = 1;
        $list = Log::getList($request)['list'];

        $row = [
            ['table_header' => '旧料流水信息'],
            [
                'start_store_name' => '起始门店',
                'target_store_name' => '目的门店',
                'order_time' => '订单时间',
                'order_sncode' => '关联订单号',
                'goods_category_id' => '旧料类别',
                'exchange_amount' => '回收金额',
                'piece_weight' => '重量',
                'stock_type' => '回收方式',
                //'back_company_price' => '旧料估值',
                //'old_recovery_price' => '回收金价',
                'count' => '数量',
                'cf_no' => '证书号',
                'log_type' => '类型',
                'create_at' => '操作时间',
                'create_account_real_name' => '操作人'

            ]];

        $exceldata = [];
        foreach ($list as $val) {
            switch ($val['log_type']) {
                case 1:
                    $log_type = '在库';
                    break;
                case 2:
                    $log_type = '调拨中';
                    break;
                case 3:
                    $log_type = '调拨';
                    break;
                case 4:
                    $log_type = '调拨取消';
                    break;
                case 5:
                    $log_type = '返厂中';
                    break;
                case 6:
                    $log_type = '返厂';
                    break;
                case 7:
                    $log_type = '返厂取消';
                    break;
                case 8:
                    $log_type = '回收入库';
                    break;
                case 9:
                    $log_type = '截金入库';
                    break;
            }

            $exceldata[] = [
                'start_store_name' => !empty($val['startstoreinfo']['store_name']) ? $val['startstoreinfo']['store_name'] : '',
                'target_store_name' => !empty($val['targetstoreinfo']['store_name']) ? $val['targetstoreinfo']['store_name'] : '',
                'order_sncode' => (string)$val['order_sncode'],
                'create_at' => date('Y-m-d H:i:s', $val['create_at']),
                'goods_category_id' => !empty($val['goods_category']['category_name']) ? $val['goods_category']['category_name'] : '',
                'stock_type' => ($val['stock_type'] == 1) ? '按克' : '标价',
                'back_company_price' => $val['back_company_price']/100,
               // 'old_recovery_price' => $val['old_recovery_price'],
                'count' => $val['count'],
                'piece_weight' => $val['piece_weight'],
                'cf_no' => $val['cf_no'],
                'log_type' => $log_type,
                'exchange_amount' => $val['exchange_amount']/100,
                'order_time'=>date('Y-m-d H:i:s', $val['create_at']),
                'create_account_real_name'=>$val['create_account_real_name']

            ];
        }

        $filename = '旧料流水信息导出_' . time() . '.xlsx';
        return Excel::download(new CommonExcelExport($row, $exceldata), $filename);
    }

}
